package com.henu.cee_service.utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
//import net.sf.json.JSONObject;

public class SchoolRanking {

	private static String filePath = "E:\\3_code\\1_java\\project\\cee\\cee-service\\src\\main\\java\\com\\henu\\cee_service\\utils\\一批数据.xls";

	private static List<String> schoolIdList; // 院校序号
    private static List<String> schoolList;   // 院校名称
    private static List<Integer> scoreList;   // 2018年高考分数
    private static List<Integer> rankList;    // 2018年高考位次

    /**
     * 读取第一批数据excel文件
     * @param filePath：文件路径
     * @return
     */
    public static void readExcel(String filePath) {

        try {
            Workbook wb = null;
            InputStream is = new FileInputStream(filePath);
            wb = Workbook.getWorkbook(is);

            schoolIdList = new ArrayList<>();
            schoolList = new ArrayList<>();
            scoreList = new ArrayList<>();
            rankList = new ArrayList<>();

            int sheetSize = wb.getNumberOfSheets();
            Sheet sheet = wb.getSheet(0);

            //获取excel的总行数
            int rowTotal = sheet.getRows();
            //除去标题行的数据总量
            for (int row = 1; row < rowTotal; row++) {

                Cell[] cells = sheet.getRow(row);

                schoolIdList.add(cells[0].getContents());
                schoolList.add(cells[1].getContents());
                try{
                    scoreList.add(Integer.parseInt(cells[2].getContents()));
                }catch (Exception e){
                    int score = -1;
                    scoreList.add(score);
                }
                try{
                    rankList.add(Integer.parseInt(cells[3].getContents()));
                }catch (Exception e){
                    int rank = -1;
                    rankList.add(rank);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据高考分数，进行院校推荐
     * @param score
     * @return
     */
    public static Map<String, String> schoolRecommendation(int score){
        /**
         * 获取当前分数索引
         */
        Boolean flag = false;
        int tmp = 0;

        while(!flag){
            for(int i=0; i<scoreList.size(); i++) {
                if (score == scoreList.get(i)) {
                    tmp = i;
                    flag = true;
                    break;
                }
            }
            if(!flag) score--;
        }

        Map<String, String> result = new HashMap<String, String>();
        //当该高考分数查询不到，返回异常代码
        if(tmp == 0){
            result.put("error", "对不起，查找不到该分数段位的院校！");
            return result;
        }
        /**
         * 根据分数索引，查询到位次并将相近的6所院校加入
         */
        int inx = 1;
        for(int i=tmp-2; i<=tmp+3; i++){
            result.put("schoolId"+inx, schoolIdList.get(i));
            result.put("schoolName"+inx, schoolList.get(i));
            result.put("schoolAdmissionScore"+inx, scoreList.get(i).toString());
            result.put("studentRank"+inx, rankList.get(i).toString());
            inx++;
        }

        return result;
    }


    public static Map<String, String> getSchoolByScore(int score)
    {
        readExcel(filePath);
        Map<String, String> res = schoolRecommendation(score);
        return res;
    }

//    public static void main(String[] args) {
//        int score = 567;
//        Map<String, String> res = getSchoolByScore(score);
//        System.out.println(res);
//
//    }
}
